﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using AutoMapper.QueryableExtensions;
using FwProject.Core.TypeNumber;
using FwProject.Manager;
using FwProject.Restaurant;
using FwProject.Restaurant.Dto;
using FwProject.TypeNumber.Dto;
using X.PagedList;

namespace FwProject.TypeNumber
{
    [AbpAuthorize]
    public class TypeNumberInfoAppService : FwProjectAppServiceBase, ITypeNumberInfoAppService
    {
        public readonly IRepository<TypeNumberInfo, Guid> _repositoryTypeNumberInfo;
        private readonly TypeNumberInfoManager _typeNumberInfoManager;

        public readonly ICacheManager _catchmanager;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }
        public TypeNumberInfoAppService(
            ICacheManager catchmanager, IRepository<TypeNumberInfo, Guid> repositoryTypeNumberInfo,
            TypeNumberInfoManager typeNumberInfoManager)
        {
            _catchmanager = catchmanager;
            _repositoryTypeNumberInfo = repositoryTypeNumberInfo;
            _typeNumberInfoManager = typeNumberInfoManager;
            _guid = SequentialGuidGenerator.Instance;
        }

        /// <summary>
        /// 创建一个型号
        /// </summary>
        /// <returns>The create.</returns>
        /// <param name="input">Input.</param>
        public async Task<Guid> Create(TypeNumberInfoInput input)
        {
            var user = (await GetCurrentUserAsync());
            var model = _map.Map<TypeNumberInfo>(input);
            model.Id = _guid.Create();
            model.FactoryinfoId = user.FactoryinfoId;
            return await _typeNumberInfoManager.CreateTypenumber(model);
        }

        /// <summary>
        /// 获取一个型号
        /// </summary>
        /// <returns>The by identifier async.</returns>
        /// <param name="id">Identifier.</param>
        public async Task<TypeNumberInfoDto> GetByIdAsync(Guid id)
        {
            var model = await _repositoryTypeNumberInfo.GetAsync(id);
            return _map.Map<TypeNumberInfoDto>(model);

        }
        /// <summary>
        /// 获取型号列表
        /// </summary>
        /// <returns>The restaurant paged list async.</returns>
        /// <param name="pageIdx">Page index.</param>
        /// <param name="pageSize">Page index.</param>
        /// <param name="begindateTime">Page index.</param>
        /// <param name="enddatetime">Page index.</param>
        public async Task<IPagedList<TypeNumberInfoDto>> GetPagedListAsync(int pageIdx, int pageSize, DateTime? begindateTime, DateTime? enddatetime, string factoryid)
        {
            var listmodel = (await GetCache).AsQueryable();
            if (begindateTime.HasValue)
                listmodel= listmodel.Where(o => o.CreationTime >= begindateTime);

            if (enddatetime.HasValue)
                listmodel= listmodel.Where(o => o.CreationTime <= enddatetime);

            if (!string.IsNullOrWhiteSpace(factoryid))
            {
                Guid fid = new Guid(factoryid);
                listmodel= listmodel.Where(o => o.FactoryinfoId == fid);
            }

            return await listmodel.ProjectTo<TypeNumberInfoDto>().OrderByDescending(m => m.CreationTime).ToPagedListAsync(pageIdx, pageSize);

        }
        /// <summary>
        /// 修改型号信息
        /// </summary>
        /// <returns>The update.</returns>
        /// <param name="id">Identifier.</param>
        /// <param name="input">Input.</param>
        public async Task Update(Guid id, TypeNumberInfoInput input)
        {
            var model = await _repositoryTypeNumberInfo.GetAsync(id);
            model = _map.Map(input, model);
            await _typeNumberInfoManager.UpdateTypenumber(model);

        }

        /// <summary>
        /// 删除型号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task Delete(Guid id)
        {
            var model = await _repositoryTypeNumberInfo.GetAsync(id);
            await _typeNumberInfoManager.DeleteTypenumber(model);
        }
        /// <summary>
        /// 获取该工厂中的所有型号
        /// </summary>
        /// <returns></returns>
        public async Task<List<TypeNumberInfoDto>> GetAllTypeNumber()
        {
            var user = await UserManager.GetUserByIdAsync(AbpSession.UserId.Value);
            return _map.Map<List<TypeNumberInfoDto>>((await GetCache).Where(o =>o.FactoryinfoId == user.FactoryinfoId).OrderByDescending(m => m.CreationTime));
        }
        /// <summary>
        /// 获取整个系统中的型号
        /// </summary>
        /// <returns></returns>
        public async Task<List<TypeNumberInfoDto>> GetAllSystemTypeNumber()
        {
            return _map.Map<List<TypeNumberInfoDto>>((await GetCache));
        }
   


        /// <summary>
        /// App获取工厂默认型号
        /// </summary>
        /// <returns></returns>
        public async Task<List<TypeNumberInfoDto>> GetDefaultTypeNumber()
        {
            var user = await UserManager.GetUserByIdAsync(AbpSession.UserId.Value);
            return _map.Map<List<TypeNumberInfoDto>>((await GetCache).Where(o=>o.IsActive==true && o.FactoryinfoId == user.FactoryinfoId).OrderByDescending(m => m.CreationTime));
        }
        /// <summary>
        /// 获取不是工厂默认型号
        /// </summary>
        /// <returns></returns>
        public async Task<List<TypeNumberInfoDto>> GetNotDefaultTypeNumber()
        {
            var user =await UserManager.GetUserByIdAsync(AbpSession.UserId.Value);
            return _map.Map<List<TypeNumberInfoDto>>((await GetCache).Where(o=>o.IsActive== false &&  o.FactoryinfoId==user.FactoryinfoId ).OrderByDescending(m => m.CreationTime));
        }
        private Task<List<TypeNumberInfo>> GetCache
        {
            get
            {
                ICache cache = _catchmanager.GetCache(nameof(TypeNumberInfo));
                return cache.GetAsync("list", async () => await _repositoryTypeNumberInfo.GetAllListAsync());
            }

        }
    }
}
